#include "StdAfx.h"
#include "FMDBParameter.h"
#include "DBTOOLS.h"
#include <assert.h>

//note:here VarValueArray is a void* array, this is usful when caller want to run a series of DML statement
// for example if caller want to update or insert a set of record, the SQL statement could be the same except binding variable.
// use parameter array, we can get best performace for batch update,insert,etc
FMDBParameter::FMDBParameter( TCHAR *VarName, int *VarValueArray )
{
	m_pIndicator=NULL;
	Set(VarName,VarValueArray);

}

FMDBParameter::FMDBParameter( TCHAR *VarName,TCHAR *VarValueArray )
{
	m_pIndicator=NULL;
	Set(VarName,VarValueArray);

}

FMDBParameter::FMDBParameter( TCHAR *VarName,long *VarValueArray )
{
	m_pIndicator=NULL;
	Set(VarName,VarValueArray);

}

FMDBParameter::FMDBParameter( TCHAR *VarName,double *VarValueArray )
{
	m_pIndicator=NULL;
	Set(VarName,VarValueArray);
}

FMDBParameter::FMDBParameter( void )
{
	m_pstrVarName=NULL;
	m_VarValue=NULL;
	m_pIndicator=NULL;
}

void FMDBParameter::Set( TCHAR *VarName,void* VarValueArray, ub2 ub2VarDataType )
{
	DBTOOLS::CopyPropertyValue(&m_pstrVarName,VarName);
	m_VarValue = VarValueArray;

	m_VarDataType = ub2VarDataType;
}

void FMDBParameter::Set( TCHAR *VarName,double *VarValueArray )
{
	DBTOOLS::CopyPropertyValue(&m_pstrVarName,VarName);
	m_VarValue=VarValueArray;

	m_VarDataType=SQLT_BDOUBLE;
}

void FMDBParameter::Set( TCHAR *VarName,TCHAR *VarValueArray )
{
	DBTOOLS::CopyPropertyValue(&m_pstrVarName,VarName);
	m_VarValue=VarValueArray;

	m_VarDataType=SQLT_STR;
}

void FMDBParameter::Set( TCHAR *VarName,long *VarValueArray )
{
	DBTOOLS::CopyPropertyValue(&m_pstrVarName,VarName);
	m_VarValue=VarValueArray;

	m_VarDataType=SQLT_INT;
}

void FMDBParameter::Set( TCHAR *VarName,int *VarValueArray )
{
	DBTOOLS::CopyPropertyValue(&m_pstrVarName,VarName);
	m_VarValue=VarValueArray;

	m_VarDataType=SQLT_INT;
}


FMDBParameter::~FMDBParameter(void)
{
	free(m_pstrVarName);
	//parameter do not release storage memory, the caller should release it
}

FMDBParameter* FMDBParameters::AddParam(TCHAR *VarName, void* pVarValueArray, ub2 ub2VarDataType, ub4 ub4Size, short* pIndicator)
{
	ub2 ub2DataTypeConverted = ub2VarDataType;

	FMDBParameter* pDBParameter = new FMDBParameter;
	pDBParameter->Set(VarName, pVarValueArray, ub2DataTypeConverted);
	pDBParameter->SetVarSize(ub4Size);
	pDBParameter->SetIndicator(pIndicator);//normally string do not need indicator

	m_vParam.push_back(pDBParameter);
	return pDBParameter;
}

//void FMDBParameters::AddOCIIOVParam( FMDBIOVParameter *Param )
//{
//	//only check the para name should not be NULL
//	if (Param)
//	{		
//		m_vIOVParam.push_back(Param);		
//	}
//}


void FMDBParameters::Clear()
{
	for(std::vector<FMDBParameter *>::iterator iter = m_vParam.begin();iter != m_vParam.end(); iter++)
	{
		delete *iter;
	}
	for(std::vector<FMDBIOVParameter *>::iterator iter = m_vIOVParam.begin();iter != m_vIOVParam.end(); iter++)
	{
		delete *iter;
	}
	for(std::vector<FMDBCursorParameter *>::iterator iter = m_vCursorParam.begin();iter != m_vCursorParam.end(); iter++)
	{
		delete *iter;
	}
	for(std::vector<FMDBCLOBParameter *>::iterator iter = m_vCLOBParam.begin();iter != m_vCLOBParam.end(); iter++)
	{
		delete *iter;
	}
	m_vParam.clear();
	m_vIOVParam.clear();
	m_vCursorParam.clear();
	m_vCLOBParam.clear();
}

FMDBParameters::~FMDBParameters( void )
{
	Clear();
}

FMDBCursorParameter* FMDBParameters::AddCursorParam(TCHAR *VarName)
{
	FMDBCursorParameter *pCursorParam=new FMDBCursorParameter(VarName);
	m_vCursorParam.push_back(pCursorParam);	
	return pCursorParam; 
}

FMDBCLOBParameter* FMDBParameters::AddCLOBParam( TCHAR *VarName )
{
	FMDBCLOBParameter *pCLOBParam=new FMDBCLOBParameter(VarName);
	m_vCLOBParam.push_back(pCLOBParam);	
	return pCLOBParam; 
}


FMDBIOVParameter::FMDBIOVParameter( TCHAR *VarName,OCIIOV *VarValueArray,ub4 ArraySize,ub2 VarOraDataType )
{
	DBTOOLS::CopyPropertyValue(&m_pstrVarName,VarName);
	m_VarValue=VarValueArray;
	m_ValueArraySize=ArraySize;
	m_VarDataType=VarOraDataType;
	//note:here I do not keep varValue,so the caller must keep the value available till finish the executing.
}


FMDBCursorParameter::FMDBCursorParameter( TCHAR *VarName)
{
	m_pstrVarName=nullptr;
	m_VarValue=nullptr;

	DBTOOLS::CopyPropertyValue(&m_pstrVarName,VarName);
}

FMDBCursorParameter::~FMDBCursorParameter( void )
{
	free (m_pstrVarName);
}

FMDBCLOBParameter::FMDBCLOBParameter( TCHAR *VarName )
{
	m_pstrVarName=NULL;
	m_VarValue=NULL;
	DBTOOLS::CopyPropertyValue(&m_pstrVarName,VarName);
}

FMDBCLOBParameter::~FMDBCLOBParameter( void )
{
	free (m_pstrVarName);
}
